package org.jeecg.modules.xmss.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.DySmsHelper;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.jeecg.modules.xmss.entity.XmssIdea;
import org.jeecg.modules.xmss.entity.XmssSupervise;
import org.jeecg.modules.xmss.entity.XmssTowns;
import org.jeecg.modules.xmss.mapper.XmssIdeaMapper;
import org.jeecg.modules.xmss.mapper.XmssSuperviseMapper;
import org.jeecg.modules.xmss.mapper.XmssTownsMapper;
import org.jeecg.modules.xmss.service.IXmssIdeaService;
import org.jeecg.modules.xmss.service.IXmssSuperviseService;
import org.jeecg.modules.xmss.service.impl.XmssTownsServiceImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

public class TimedShortNoteController implements Job {

    @Autowired
    private XmssSuperviseMapper xmssSuperviseMapper;

    @Autowired
    private IXmssSuperviseService xmssSuperviseService;

    @Autowired
    private XmssIdeaMapper xmssIdeaMapper;

    @Autowired
    private IXmssIdeaService xmssIdeaService;

    @Autowired
    private XmssTownsMapper xmssTownsMapper;

    @Autowired
    private org.jeecg.modules.system.mapper.SysUserMapper sysUserMapper;




    /**
     *  定时任务上午九点执行，查询大于七天的未读事件发短信给督办
     *
     * @param
     * @return
     */

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JSONObject obj = new JSONObject();
        //通过镇去遍历下面的事件以及人员
        LambdaQueryWrapper<XmssTowns> queryWrapperXmssTowns = new LambdaQueryWrapper<XmssTowns>();
        List<XmssTowns> getXmssTownsList=xmssTownsMapper.selectList(queryWrapperXmssTowns);
        //安装定时任务的条件去查询事件
        getXmssTownsList.stream().forEach(e -> {
            if(e.getRemind()!=null && !"".equals(e.getRemind()) &&!"0".equals(e.getRemind())){
            LambdaQueryWrapper<XmssIdea> queryWrapperXmssIdea = new LambdaQueryWrapper<XmssIdea>();
            //根据每个镇设置的提醒时间查询
            Calendar calendar1 = Calendar.getInstance();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            calendar1.add(Calendar.DATE, Integer.valueOf("-"+ e.getRemind()));
            String three_days_ago = sdf1.format(calendar1.getTime());
            //查询条件
            queryWrapperXmssIdea.lt(XmssIdea::getDbTime, three_days_ago);
                queryWrapperXmssIdea.eq(XmssIdea::getTownsId,e.getId());
            List<XmssIdea> getXmssIdeaList=xmssIdeaMapper.selectList(queryWrapperXmssIdea);
            //通过事件查询关联表
                List<String> getlistIds=getXmssIdeaList.stream().map(XmssIdea::getId).collect(Collectors.toList());
                //用于判断集合对象是否为空，如果为空，则返回 true，否则返回 false。
                if(!getlistIds.isEmpty()){
                    LambdaQueryWrapper<XmssSupervise> queryWrapperXmssSupervise = new LambdaQueryWrapper<XmssSupervise>();
                    queryWrapperXmssSupervise.in(XmssSupervise::getIdeaId,getlistIds);
                    List<XmssSupervise> getListXmssSupervise=xmssSuperviseMapper.selectList(queryWrapperXmssSupervise);
                    //通过关联表查询用户表
                    List<String> getXmssIdeaListIds=getListXmssSupervise.stream().map(XmssSupervise::getUserId).collect(Collectors.toList());
                    if(!getXmssIdeaListIds.isEmpty()){
                        LambdaQueryWrapper<SysUser> queryWrapperSysUser = new LambdaQueryWrapper<SysUser>();
                        queryWrapperSysUser.in(SysUser::getId,getXmssIdeaListIds);
                        List<SysUser> getUserList=sysUserMapper.selectList(queryWrapperSysUser);
                        getUserList.stream().forEach(j ->{
                            try {
                                System.err.println("结果："+j.getPhone());
                                DySmsHelper.sendSmsZll(j.getPhone(), obj, e.getRemindTemplateCode(), e.getSign());
                            } catch (ClientException ex) {
                                ex.printStackTrace();
                            }
                        });

                    }

                }


            }
        });

    }








}
